package com.wzq.app.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.ByteArrayOutputStream;
import java.io.File;

/**
 * Created by yszong on 2015/12/9.
 */
public class GlideUtils {

    public static void displayImage(Context context, ImageView imageView, String url ){
        Glide.with(context)
                .load(url)
                .into(imageView);
    }

    public static void displayImage(Context context, ImageView imageView, String url , int loadImageId){
        Glide.with(context)
                .load(url)
                .placeholder(loadImageId)
                .crossFade()
                .into(imageView);
    }
    public static void displayImage(Context context, ImageView imageView, String url , int loadImageId, int errorImageId){
        Glide.with(context)
                .load(url)
                .placeholder(loadImageId)
                .error(errorImageId)
                .crossFade()
                .into(imageView);
    }

//    public static void displayImage(Context context, final ImageView imageView, final String url, final PhotoViewActivity.OnLoadCompleteListener listener){
//        Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
//            @Override
//            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                imageView.setImageBitmap(resource);
//                listener.OnLoadCompleteSucess(resource);
//            }
//        });
//    }
//
//    public static void displayImage(Context context, final ImageView imageView, String url , int loadImageId, int errorImageId, final PhotoViewActivity.OnLoadCompleteListener listener){
//        Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
//            @Override
//            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                imageView.setImageBitmap(resource);
//                listener.OnLoadCompleteSucess(resource);
//            }
//        });
//    }

    public static void displayImage(Context context, ImageView imageView, Bitmap bitmap ){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes=baos.toByteArray();

        Glide.with(context)
                .load(bytes)
                .into(imageView);
    }

    public static void displayImage(Context context, ImageView imageView, File file ){
        Glide.with(context)
                .load(file)
                .into(imageView);
    }
    public static void displayImage(Context context, ImageView imageView, int drawableId ){
        Glide.with(context)
                .load(drawableId)
                .into(imageView);
    }

    /**
     * load normal  for img
     *
     * @param url
     * @param erroImg
     * @param emptyImg
     * @param iv
     */
    public static void glideLoader(Context context, String url, int erroImg, int emptyImg, ImageView iv) {
        //原生 API
        Glide.with(context).load(url).placeholder(emptyImg).error(erroImg).into(iv);
    }
    /**
     * load normal  for  circle or round img
     *
     * @param url
     * @param erroImg
     * @param emptyImg
     * @param iv
     * @param type 形状
     */
    public static void glideLoader(Context context, String url, int erroImg, int emptyImg, ImageView iv, ShapeType type) {
        if (ShapeType.CIRCLE == type) {
            Glide.with(context).load(url).placeholder(emptyImg).error(erroImg).transform(new GlideCircleTransform(context)).into(iv);
        } else if (ShapeType.ROUND == type) {
            Glide.with(context).load(url).placeholder(emptyImg).error(erroImg).transform(new GlideRoundTransform(context,10)).into(iv);
        }
    }
}
